﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace source
{
    public partial class KorisnikKontrola : UserControl
    {
        //Atributi za spremanje kontrola sa formi
        private Button b1, b2, b3, b4, b5;
        
        //Zajednicki panel za prikaz rezultata pretrage
        private FlowLayoutPanel flow;
        
        //Varijabla koja osigurava da se svakom promjenom
        //teksta ne ucitavaju vrijednosti iz baze ponovo
        //u liste
        bool pedagogUcitan = false;
        bool direktorUcitan = false;
        bool ucenikUcitan = false;
        bool profesorUcitan = false;
        
        //Liste za pretragu, učitavaju se iz baze
        List<Pedagog> pedagozi = new List<Pedagog>();
        List<Profesor> profesori = new List<Profesor>();
        List<Direktor> direktori = new List<Direktor>();
        List<Ucenik> ucenici = new List<Ucenik>();

        private String userTip; 
        
        
        public KorisnikKontrola()
        {
            InitializeComponent();
        }
        //Funkcija koja, shodno formi koja je poziva, 
        //dodjeljuje event za button Home
        public void postaviEvent()
        {
            if (userTip == "Direktor")
            {
                this.toolStripButton1.Click += new EventHandler(this.toolStripButton1_Click);
            }
            else if (userTip == "Pedagog")
            {
                this.toolStripButton1.Click += new EventHandler(this.toolStripButton1_ClickPedagog);
            }
            else if (userTip == "Profesor")
            {
                this.toolStripButton1.Click += new EventHandler(this.toolStripButton1_ClickProfesor);
            }
            else if (userTip == "Ucenik")
            {
                this.toolStripButton1.Click += new EventHandler(this.toolStripButton1_ClickProfesor);
            }
            else if(userTip == "Administrator")
            {
                this.toolStripButton1.Click += new EventHandler(this.toolStripButton1_ClickAdmin);
            }
        }
        //Postavljanje korisničkog tipa, tj. o kojoj formi je riječ
        public void postaviKorisnickiTip(String tip)
        {
            this.userTip = tip;
        }
        //Event click buttona za odjavu
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            this.ParentForm.Close();
        }
        //Spremanje flowLayoutPanela za pretragu
        public void ucitajFlow(FlowLayoutPanel f)
        {
            this.flow = f;
        }
        //Postavljanje korisničkog imena u labelu
        public void postaviUser(string u)
        {
            label1.Text = u;
        }
        #region EventiZaCheckButton
        private void poToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
        {
            if (this.poToolStripMenuItem.Checked)
            {
                this.poKorisnicimaToolStripMenuItem.Checked = false;
                this.pToolStripMenuItem.Checked = false;
                this.pedagogToolStripMenuItem.Checked = false;
            }
        }
        private void pToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
        {
            if (this.pToolStripMenuItem.Checked)
            {
                this.poKorisnicimaToolStripMenuItem.Checked = false;
                this.poToolStripMenuItem.Checked = false;
                this.pedagogToolStripMenuItem.Checked = false;
            }
        }
        private void poKorisnicimaToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
        {
            if (this.poKorisnicimaToolStripMenuItem.Checked)
            {
                this.pToolStripMenuItem.Checked = false;
                this.poToolStripMenuItem.Checked = false;
                this.pedagogToolStripMenuItem.Checked = false;
            }
        }
        private void pedagogToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
        {
            if (this.pedagogToolStripMenuItem.Checked)
            {
                this.pToolStripMenuItem.Checked = false;
                this.poToolStripMenuItem.Checked = false;
                this.poKorisnicimaToolStripMenuItem.Checked = false;
            }
        }
        #endregion
        //Pretraga
        private void toolStripTextBox1_TextChanged(object sender, EventArgs e)
        {
            #region Pretraga
            if (this.pedagogToolStripMenuItem.Checked)
            {
                this.flow.Controls.Clear();
                this.flow.Size = new System.Drawing.Size(1200, 5);

                if (!pedagogUcitan)
                {
                    DAL.DAL dal = DAL.DAL.Instanca;
                    dal.kreirajKonekciju("127.0.0.1", "skola", "root", "");
                    DAL.DAL.PedagogDAO p = new DAL.DAL.PedagogDAO();
                    pedagozi = p.getAll();
                    dal.terminirajKonekciju();
                    pedagogUcitan = true;
                }
                if (pedagozi.Count == 0)
                    return;
                this.flow.Size = new System.Drawing.Size(1200, ((pedagozi.Count/10)+1)*160);
                foreach (Pedagog pg in pedagozi)
                {
                    if (pg.Ime.Contains(toolStripTextBox1.Text) || pg.Prezime.Contains(toolStripTextBox1.Text) || pg.Username.Contains(toolStripTextBox1.Text))
                    {
                        KontrolaZaPretragu kp = new KontrolaZaPretragu();
                        kp.spremiPedagoga(pg);
                        kp.dodajTip(pg.Tip);
                        kp.vrstaPretrage("Pedagog");
                        kp.popuniVrijednosti(pg.Ime, pg.Prezime);
                        this.flow.Controls.Add(kp);
                    }
                }
            }
            else if (this.poToolStripMenuItem.Checked)
            {
                this.flow.Controls.Clear();
                this.flow.Size = new System.Drawing.Size(1204, 5);

                if (!direktorUcitan)
                {
                    DAL.DAL dal = DAL.DAL.Instanca;
                    dal.kreirajKonekciju("127.0.0.1", "skola", "root", "");
                    DAL.DAL.DirektorDAO p = new DAL.DAL.DirektorDAO();
                    direktori = p.getAll();
                    dal.terminirajKonekciju();
                    direktorUcitan = true;
                }
                if (direktori.Count == 0)
                    return;
                this.flow.Size = new System.Drawing.Size(1200, ((direktori.Count / 10) + 1) * 160);
                foreach (Direktor pg in direktori)
                {
                    if (pg.Ime.Contains(toolStripTextBox1.Text) || pg.Prezime.Contains(toolStripTextBox1.Text) || pg.Username.Contains(toolStripTextBox1.Text))
                    {
                        KontrolaZaPretragu kp = new KontrolaZaPretragu();
                        kp.spremiDirektora(pg);
                        kp.dodajTip(pg.Tip);
                        kp.vrstaPretrage("Direktor");
                        kp.popuniVrijednosti(pg.Ime, pg.Prezime);
                        this.flow.Controls.Add(kp);
                    }
                }
            }
            else if (this.pToolStripMenuItem.Checked)
            {
                this.flow.Controls.Clear();
                this.flow.Size = new System.Drawing.Size(1204, 5);

                if (!profesorUcitan)
                {
                    DAL.DAL dal = DAL.DAL.Instanca;
                    dal.kreirajKonekciju("127.0.0.1", "skola", "root", "");
                    DAL.DAL.ProfesorDAO p = new DAL.DAL.ProfesorDAO();
                    profesori = p.getAll();
                    dal.terminirajKonekciju();
                    profesorUcitan = true;
                }
                if (profesori.Count == 0)
                    return;
                this.flow.Size = new System.Drawing.Size(1200, ((profesori.Count / 10) + 1) * 160);
                foreach (Profesor pg in profesori)
                {
                    if (pg.Ime.Contains(toolStripTextBox1.Text) || pg.Prezime.Contains(toolStripTextBox1.Text) || pg.Username.Contains(toolStripTextBox1.Text))
                    {
                        KontrolaZaPretragu kp = new KontrolaZaPretragu();
                        kp.spremiProfesora(pg);
                        kp.dodajTip(pg.Tip);
                        kp.vrstaPretrage("Profesor");
                        kp.popuniVrijednosti(pg.Ime, pg.Prezime);
                        this.flow.Controls.Add(kp);
                    }
                }
            }
            else if (this.poKorisnicimaToolStripMenuItem.Checked)
            {
                this.flow.Controls.Clear();
                this.flow.Size = new System.Drawing.Size(1204, 5);

                if (!ucenikUcitan)
                {
                    DAL.DAL dal = DAL.DAL.Instanca;
                    dal.kreirajKonekciju("127.0.0.1", "skola", "root", "");
                    DAL.DAL.UcenikDAO p = new DAL.DAL.UcenikDAO();
                    ucenici = p.getAll();
                    dal.terminirajKonekciju();
                    ucenikUcitan = true;
                }
                if (ucenici.Count == 0)
                    return;
                this.flow.Size = new System.Drawing.Size(1200, ((ucenici.Count / 10) + 1) * 160);
                foreach (Ucenik pg in ucenici)
                {
                    if (pg.Ime.Contains(toolStripTextBox1.Text) || pg.Prezime.Contains(toolStripTextBox1.Text) || pg.Username.Contains(toolStripTextBox1.Text))
                    {
                        KontrolaZaPretragu kp = new KontrolaZaPretragu();
                        kp.spremiUcenika(pg);
                        kp.dodajTip(pg.Tip);
                        kp.popuniVrijednosti(pg.Ime, pg.Prezime);
                        this.flow.Controls.Add(kp);
                    }
                }
            }
            else
                return;
            #endregion
        }
        #region razlicitiEventi
        //Event za Direktor formu
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.ParentForm.Refresh();
            this.toolStripTextBox1.Text = "";
            this.flow.Controls.Clear();
            this.flow.Size = new System.Drawing.Size(1200, 5);
            if (b1.Text[0] == '-')
                b1.PerformClick();
            if (b2.Text[0] == '-')
                b2.PerformClick();
            if (b3.Text[0] == '-')
                b3.PerformClick();
            if (b4.Text[0] == '-')
                b4.PerformClick();
            if (b5.Text[0] == '-')
                b5.PerformClick();
            pedagogUcitan = false;
            ucenikUcitan = false;
            profesorUcitan = false;
            direktorUcitan = false;

        }
        //Event za Pedagog formu
        private void toolStripButton1_ClickPedagog(object sender, EventArgs e)
        {
            this.ParentForm.Refresh();
            this.toolStripTextBox1.Text = "";
            this.flow.Controls.Clear();
            this.flow.Size = new System.Drawing.Size(1200, 5);
            if (b1.Text[0] == '-')
                b1.PerformClick();
            if (b2.Text[0] == '-')
                b2.PerformClick();
            if (b3.Text[0] == '-')
                b3.PerformClick();
            if (b4.Text[0] == '-')
                b4.PerformClick();
            pedagogUcitan = false;
            ucenikUcitan = false;
            profesorUcitan = false;
            direktorUcitan = false;

        }
        //Event za Profesor formu
        private void toolStripButton1_ClickProfesor(object sender, EventArgs e)
        {
            this.ParentForm.Refresh();
            this.toolStripTextBox1.Text = "";
            this.flow.Controls.Clear();
            this.flow.Size = new System.Drawing.Size(1200, 5);
            if (b1.Text[0] == '-')
                b1.PerformClick(); 
            if (b2.Text[0] == '-')
                b2.PerformClick();
            if (b3.Text[0] == '-')
                b3.PerformClick();
            pedagogUcitan = false;
            ucenikUcitan = false;
            profesorUcitan = false;
            direktorUcitan = false;

        }
        //Event za Administrator formu
        private void toolStripButton1_ClickAdmin(object sender, EventArgs e)
        {
            this.ParentForm.Refresh();
            this.toolStripTextBox1.Text = "";
            this.flow.Controls.Clear();
            this.flow.Size = new System.Drawing.Size(1200, 5);
            if (b1.Text[0] == '-')
                b1.PerformClick();
            if (b2.Text[0] == '-')
                b2.PerformClick();
            if (b3.Text[0] == '-')
                b3.PerformClick();
            if (b4.Text[0] == '-')
                b4.PerformClick();
            pedagogUcitan = false;
            ucenikUcitan = false;
            profesorUcitan = false;
            direktorUcitan = false;
        }
        #endregion

        #region SpremanjeKontrola
        //Spremi kontrole iz Direktor forme
        public void spremiKontrole(Button b1, Button b2, Button b3, Button b4, Button b5)
        {
            this.b1 = b1;            this.b2 = b2;
            this.b3 = b3;            this.b4 = b4;
            this.b5 = b5;   
        }
        //Spremi kontrole iz Profesor forme u Ucenik forme
        public void spremiKontrole(Button button1, Button button2, Button button3)
        {
            this.b1 = button1; this.b2 = button2;
            this.b3 = button3; 
        }
        //Spremi kontrole iz Pedagog forme
        public void spremiKontrole(Button button1, Button button2, Button button3, Button button4)
        {
            this.b1 = button1; this.b2 = button2;
            this.b3 = button3; this.b4 = button4;
        }
        #endregion
    }
}

    